package com.open.free.generator.controller;


import com.open.free.generator.base.BaseController;
import com.open.free.generator.constant.TemplateConstants;
import com.open.free.generator.project.CoreClassGenerator;
import com.open.free.generator.project.ProjectPomInit;
import com.open.free.generator.project.ProjectSrcGenerator;
import com.open.free.generator.result.ResultData;
import com.open.free.generator.vo.InitProjectVo;
import freemarker.template.TemplateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;


/**
 * @author Faye.Wang
 * @version 1.0
 * @date 2020/12/15 21:46
 * @Description
 */
@Controller
public class InitializrController extends BaseController {
    @Autowired
    private ProjectPomInit projectPomInit;
    @Autowired
    private ProjectSrcGenerator projectSrcGenerator;
    @Autowired
    private CoreClassGenerator coreClassGenerator;
    @Value("${apache.maven.version}")
    private String mavenVersion;
    @Value("${maven.wrapper.version}")
    private String mavenWrapper;

    /**
     * init page when the user open localhost:8080
     * @return
     */
    @RequestMapping("/init")
    public String init(){

        return "init_index";
    }

    /**
     * init project and generator project workspace for springboot
     * @param initProjectVo
     * @return
     */
    @RequestMapping("/initProject")
    @ResponseBody
    public ResultData initProject(InitProjectVo initProjectVo) {
        try {
            //工程主目录
            String mainProjectPath = projectPomInit.initProject(initProjectVo,TemplateConstants.PROJECT_MAIN_POM);
            //生成core包
            String corePath = projectPomInit.initDependencyProject(initProjectVo,mainProjectPath,TemplateConstants.PROJECT_CORE_POM,"core");

            String corePackagePath = projectSrcGenerator.createSrcPackage(corePath,initProjectVo.getPackageNameValue(),"core");
            //根据最终路径包名 生成旗下的工具类包和工具类
            coreClassGenerator.coreGenerator(initProjectVo,corePackagePath,TemplateConstants.CORE_DATA_JSON);
            //生成common包
            String commonPath = projectPomInit.initDependencyProject(initProjectVo,mainProjectPath,TemplateConstants.PROJECT_COMMON_POM,"common");

            String commonPackagePath = projectSrcGenerator.createSrcPackage(commonPath,initProjectVo.getPackageNameValue(),"common");
            coreClassGenerator.coreGenerator(initProjectVo,commonPackagePath,TemplateConstants.COMMON_DATA_JSON);

            //生成 API Web 工程
            String webApiPath = projectPomInit.initDependencyProject(initProjectVo,mainProjectPath,TemplateConstants.PROJECT_API_POM,"api");
            projectPomInit.createMvnConfig(mavenVersion,mavenWrapper,webApiPath);
            // 生成SRC
            String mainPath = projectSrcGenerator.createSrcPackage(webApiPath,initProjectVo.getPackageNameValue(),"api");

            projectPomInit.createMainRunJava(StringUtils.substringBeforeLast(mainPath, File.separator),initProjectVo,TemplateConstants.PROJECT_API_SPRINGBOOT_MAIN);

            //Resources 目录
            String resourcesPath = projectSrcGenerator.createSrcResourcesPackage(webApiPath);
            coreClassGenerator.propertiesGenerator(resourcesPath,initProjectVo);

            if("Y".equals(initProjectVo.getModuleType())){
                // 生成基础模块功能
                initProjectVo.setPackageName(initProjectVo.getPackageNameValue()+".api");
                coreClassGenerator.baseModelCode(mainPath,initProjectVo,resourcesPath);
            }

            return super.resultSuccess();
        }catch (Exception e) {
            e.printStackTrace();
            return super.resultFail();
        }

    }

}
